package 人事管理系统.GUI;

import 人事管理系统.Connect.Employee_Database;
import 人事管理系统.Tables.Employee;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class emp_index {
    public emp_index(){
        JFrame jf = new JFrame("员工管理");
        jf.setBounds(350, 200, 800, 500);

        // 添加面板
        jf.setVisible(true);
        jf.setLayout(null);

        JPanel contentPane = new JPanel();
        jf.setContentPane(contentPane);
        //添加goodsNum查找框
        JTextField queryNum = new JTextField(20);
        queryNum.setText("该处输入待查找/删除/修改的员工编号：");

        queryNum.setBounds(100,20,165,25);
        contentPane.add(queryNum);
        JButton query=new JButton("查找");
        contentPane.add(query);
        query.setBackground(Color.PINK);
        JButton add=new JButton("增加");
        contentPane.add(add);
        add.setBackground(Color.PINK);
        JButton modify=new JButton("修改");
        contentPane.add(modify);
        modify.setBackground(Color.PINK);
        JButton delete=new JButton("删除");
        contentPane.add(delete);
        delete.setBackground(Color.PINK);
        JButton load=new JButton("加载");
        contentPane.add(load);
        load.setBackground(Color.PINK);

        JTextArea jta=new JTextArea("该处显示查找内容",1,30);
        jta.setLineWrap(true);    //设置文本域中的文本为自动换行
        jta.setForeground(Color.BLACK);    //设置组件的背景色
        jta.setFont(new Font("楷体",Font.BOLD,16));    //修改字体样式
        jta.setBackground(Color.pink);    //设置按钮背景色
        contentPane.add(jta);
        //文本框区域
        Object[][] tableDate=new Object[100][6];
        for(int i=0;i<20;i++)
        {
            tableDate[i][0]="";
            for(int j=1;j<6;j++)
            {
                tableDate[i][j]="";
            }
        }

        String[] name={"ENO","ENAME","SEX","education_background","Dno","Duty"};
        JTable table=new JTable(tableDate,name);
        contentPane.add(new JScrollPane(table));


        //加载按钮监听事件
        load.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Employee_Database employee_database= new Employee_Database();
                ArrayList<Employee> list=new ArrayList<>();
                list=employee_database.findAll();

                int num=1;
                //先清空
                for(int i=0;i<100;i++)
                {
                    tableDate[i][0]="";
                    for(int j=1;j<6;j++)
                    {
                        tableDate[i][j]="";
                    }
                }
                //再读取数据库中数据
                for(Employee g:list)
                {
                    tableDate[num][0]=g.getEno();
                    tableDate[num][1]=g.getEname();
                    tableDate[num][2]=g.getSex();
                    tableDate[num][3]=g.getEducation();
                    tableDate[num][4]=g.getDno();
                    tableDate[num][5]=g.getDuty();
                    num++;
                }
                table.repaint();
            }
        });

        //查找监听事件
        query.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Employee_Database emd= new Employee_Database();
                String Eno=new String();
                Eno=(queryNum.getText());
                Employee emp;
                emp=emd.findByEno(Eno);
                jta.setText(emp.getDno()+"\n"+emp.getEname()+"\n"+emp.getSex()+"\n"+emp.getEducation()+"\n"+emp.getDno()+"\n"+emp.getDuty());
            }
        });

        //删除监听
        delete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Employee_Database employee_database= new Employee_Database();
                String eno=new String();
                eno=(queryNum.getText());
                int result=employee_database.delete(eno);
            }
        });

        //修改事件
        modify.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Employee_Database employee_database= new Employee_Database();
                String eno=new String();
                eno=(queryNum.getText());
                Employee g=employee_database.findByEno(eno);
                g.setEno(tableDate[0][0].toString());
                g.setEname(tableDate[0][1].toString());
                g.setSex(tableDate[0][2].toString());
                g.setEducation(tableDate[0][3].toString());
                g.setDno(tableDate[0][4].toString());
                g.setDuty(tableDate[0][5].toString());
                employee_database.updata(g);

            }
        });

//增添事件
        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Employee_Database employee_database= new Employee_Database();
                Employee employee=new Employee();
                employee.setEno(tableDate[0][0].toString());
                employee.setEname(tableDate[0][1].toString());
                employee.setSex(tableDate[0][2].toString());
                employee.setEducation(tableDate[0][3].toString());
                employee.setDno(tableDate[0][4].toString());
                employee.setDuty(tableDate[0][5].toString());
                ArrayList<Employee> gg=new ArrayList<>();
                gg=employee_database.findAll();
                boolean flag=true;
                for(Employee gds:gg)
                {
                    if(gds.getEno().equals(employee.getEno()))
                    {
                        flag=false;
                        new opno();
                        break;
                    }
                }
                if(flag) {
                    employee_database.insert(employee);
                    new opyes();
                }

                else {
                    System.out.println("操作失败！！!");
                    new opno();
                }

            }
        });






    }
    //操作成功弹窗
    class opyes{
        public opyes(){
            // 创建 JFrame 实例
            JFrame frame = new JFrame("系统提示");
            // Setting the width and height of frame
            frame.setSize(350, 200);
            frame.setLocation(500,250);
            frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            frame.setVisible(true);
            JButton j=new JButton("操作成功！！！");
            frame.add(j);
            j.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                }
            });


        }
    }
    //操作失败弹窗
    class opno{
        public opno(){
            // 创建 JFrame 实例
            JFrame frame = new JFrame("系统提示");
            // Setting the width and height of frame
            frame.setSize(350, 200);
            frame.setLocation(500,250);
            frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            frame.setVisible(true);
            JButton j=new JButton("没有该项数据！！！");
            frame.add(j);
            j.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                }
            });}}
}
